{% extends 'base.html' %}

{% block title %}注册 - 外卖订餐系统{% endblock %}

{% block content %}
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8 col-lg-6">
            <div class="card shadow-lg">
                <div class="card-header bg-success text-white text-center py-3">
                    <h4 class="mb-0"><i class="fas fa-user-plus me-2"></i>新用户注册</h4>
                </div>
                <div class="card-body p-4">
                    <form method="post" class="needs-validation" novalidate>
                        {% csrf_token %}
                        {% if form.non_field_errors %}
                            <div class="alert alert-danger">
                                {% for error in form.non_field_errors %}
                                    <p class="mb-0">{{ error }}</p>
                                {% endfor %}
                            </div>
                        {% endif %}

                        <div class="mb-3">
                            <label for="{{ form.username.id_for_label }}" class="form-label">
                                <i class="fas fa-user me-2"></i>用户名
                            </label>
                            <input type="text" name="{{ form.username.name }}"
                                   class="form-control {% if form.username.errors %}is-invalid{% endif %}"
                                   id="{{ form.username.id_for_label }}" required
                                   placeholder="请输入用户名">
                            {% if form.username.errors %}
                                <div class="invalid-feedback">
                                    {{ form.username.errors.0 }}
                                </div>
                            {% endif %}
                            <div class="form-text">
                                <i class="fas fa-info-circle me-1"></i>用户名将用于登录，请使用字母、数字和下划线
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.email.id_for_label }}" class="form-label">
                                <i class="fas fa-envelope me-2"></i>电子邮箱
                            </label>
                            <input type="email" name="{{ form.email.name }}"
                                   class="form-control {% if form.email.errors %}is-invalid{% endif %}"
                                   id="{{ form.email.id_for_label }}" required
                                   placeholder="请输入电子邮箱">
                            {% if form.email.errors %}
                                <div class="invalid-feedback">
                                    {{ form.email.errors.0 }}
                                </div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.password1.id_for_label }}" class="form-label">
                                <i class="fas fa-lock me-2"></i>密码
                            </label>
                            <div class="input-group">
                                <input type="password" name="{{ form.password1.name }}"
                                       class="form-control {% if form.password1.errors %}is-invalid{% endif %}"
                                       id="{{ form.password1.id_for_label }}" required
                                       placeholder="请输入密码">
                                <button class="btn btn-outline-secondary" type="button" id="togglePassword1">
                                    <i class="fas fa-eye"></i>
                                </button>
                                {% if form.password1.errors %}
                                    <div class="invalid-feedback">
                                        {{ form.password1.errors.0 }}
                                    </div>
                                {% endif %}
                            </div>
                            <div class="form-text">
                                <i class="fas fa-shield-alt me-1"></i>密码必须包含至少8个字符
                            </div>
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.password2.id_for_label }}" class="form-label">
                                <i class="fas fa-lock me-2"></i>确认密码
                            </label>
                            <div class="input-group">
                                <input type="password" name="{{ form.password2.name }}"
                                       class="form-control {% if form.password2.errors %}is-invalid{% endif %}"
                                       id="{{ form.password2.id_for_label }}" required
                                       placeholder="请再次输入密码">
                                <button class="btn btn-outline-secondary" type="button" id="togglePassword2">
                                    <i class="fas fa-eye"></i>
                                </button>
                                {% if form.password2.errors %}
                                    <div class="invalid-feedback">
                                        {{ form.password2.errors.0 }}
                                    </div>
                                {% endif %}
                            </div>
                        </div>

                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-success btn-lg">
                                <i class="fas fa-user-plus me-2"></i>立即注册
                            </button>
                        </div>
                    </form>
                </div>
                <div class="card-footer text-center py-3">
                    <p class="mb-0">已有账号？
                        <a href="{% url 'login' %}" class="text-success">立即登录</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_css %}
<style>
    .card {
        border-radius: 15px;
        border: none;
    }
    .card-header {
        border-radius: 15px 15px 0 0 !important;
    }
    .form-control {
        border-radius: 8px;
        padding: 0.75rem 1rem;
    }
    .btn {
        padding: 0.75rem 1rem;
        border-radius: 8px;
    }
    .form-label {
        font-weight: 500;
    }
    .form-text {
        font-size: 0.875rem;
        color: #6c757d;
    }
    .invalid-feedback {
        font-size: 0.875rem;
    }
</style>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 密码显示切换
    function setupPasswordToggle(buttonId, inputId) {
        const toggleButton = document.querySelector(buttonId);
        const passwordInput = document.querySelector(inputId);

        if (toggleButton && passwordInput) {
            toggleButton.addEventListener('click', function() {
                const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
                passwordInput.setAttribute('type', type);
                this.querySelector('i').classList.toggle('fa-eye');
                this.querySelector('i').classList.toggle('fa-eye-slash');
            });
        }
    }

    setupPasswordToggle('#togglePassword1', '#{{ form.password1.id_for_label }}');
    setupPasswordToggle('#togglePassword2', '#{{ form.password2.id_for_label }}');

    // 表单验证
    const form = document.querySelector('form');
    form.addEventListener('submit', function(event) {
        if (!form.checkValidity()) {
            event.preventDefault();
            event.stopPropagation();
        }
        form.classList.add('was-validated');
    });
});
</script>
{% endblock %}
